<?php

/**
 * partners actions.
 *
 * @package    sf_sandbox
 * @subpackage partners
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class partnersActions extends autopartnersActions
{
    // to retrieve only this application partners
    protected function getPartnerOrCreate($id = 'id')
    {
        if (!$this->getRequestParameter($id))
        {
          $partner = new Partner();
          $partner->setProjectId(SF_APP_ID);
        }
        else
        {
          $partner = PartnerPeer::retrieveByPk($this->getRequestParameter($id));
          if( $partner->getProjectId() != SF_APP_ID )
          {
              $partner = null;
          }
          $this->forward404Unless($partner);
        }
        return $partner;
    }
    // to retrieve only this application partners
    protected function addFiltersCriteria($c)
    {
        /* @var $c Criteria */
        parent::addFiltersCriteria($c);
        $c->add(PartnerPeer::PROJECT_ID, SF_APP_ID);
        $c->addDescendingOrderByColumn(PartnerPeer::PRIORITY);
    }
  
    public function executeIndex()
    {
        $c = new Criteria();
        $c->addDescendingOrderByColumn(PartnerPeer::PRIORITY);
        $c->add(PartnerPeer::PROJECT_ID, SF_APP_ID);
        $c->add(PartnerPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $pager = new sfPropelPager('Partner', 10);
        $pager->setCriteria($c);
        $pager->setPage($this->getRequestParameter('page', 1));
        $pager->init();
        $this->pager = $pager;
    }
    
    public function executeGetlist()
    {
        $mask = trim(strip_tags($this->getRequestParameter('mask', '')));
        $len = strlen($mask);
        
        $c = new Criteria();
        //$c->add(GeoNamePeer::COUNTRY_CODE, 'LT');
        if( $len > 3 )
        {
            $c->add(GeoNamePeer::ASCII_NAME, "%" . $mask . "%", Criteria::LIKE);
            $c->addDescendingOrderByColumn(GeoNamePeer::POPULATION);
        }else 
        {
            $c->add(GeoNamePeer::ASCII_NAME, $mask . "%", Criteria::LIKE);
            $c->addDescendingOrderByColumn(GeoNamePeer::PRIORITY);
        }

        $this->geos = GeoNamePeer::doSelect($c);
    }
    
    protected function updatePartnerFromRequest()
    {
        parent::updatePartnerFromRequest();
        $this->partner->setProjectId(SF_APP_ID);
        if( !$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('photo') )
        {
            // delete old image if it was
            $filename = sfConfig::get('sf_upload_dir') . '/partners/' . $this->partner->getFilePath();
            if( is_file($filename) )
            {
                @unlink($filename);
            }
            $filename = md5($this->partner->getId() . $this->getRequest()->getFileName('photo') . date("F j, Y, g:i a")) . $this->getRequest()->getFileExtension('photo');
            $this->getRequest()->moveFile('photo', sfConfig::get('sf_upload_dir') . '/partners/' . $filename);
            $this->partner->setFilePath($filename);
        }
    }
    
    /**
     * Deletes partner from DB and its logo
     *
     * @param Partners $partners
     */
    protected function deletePartner($partner)
    {
        $filename = sfConfig::get('sf_upload_dir') . '/partners/' . $partner->getFilePath();
        if( is_file($filename) )
        {
            @unlink($filename);
        }
        $partner->delete();
    }
    
    // sorting by priority
    public function executeMoveup()
    {
        $id = (int)$this->getRequestParameter('id', 0);
        $partner = PartnerPeer::retrieveByPK($id);
        $this->forward404Unless($partner);
        if( !$id )
        {
            $this->redirect('partners/list');
        }

        $c = new Criteria();
        $c->add(PartnerPeer::PROJECT_ID, SF_APP_ID);
        APriority::moveUp($c, $id);

        $this->forward('partners', 'list');
    }
    public function executeStatus()
    {
        $id = (int)$this->getRequestParameter('id', 0);
        if(  Partner::changeStatus($id))
        {
            $this->redirect('partners/list');
        }else 
        {
            $this->forward404();
        }
    }
}
